package com.bestcem.xm.qdes.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bestcem.xm.component.mybatis.service.impl.XmServiceImpl;
import com.bestcem.xm.qdes.entity.QdesNameList;
import com.bestcem.xm.qdes.mapper.QdesNamelistMapper;
import com.bestcem.xm.qdes.service.QdesNameListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class QdesNameListServiceImpl extends XmServiceImpl<QdesNamelistMapper, QdesNameList> implements QdesNameListService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map putNameList(QdesNameList qdesNamelist) {
        Map map = new HashMap<>();
        //创建修改条件对象
        LambdaUpdateWrapper<QdesNameList> updateWrapper = new LambdaUpdateWrapper<>();
        //构造查询条件
        updateWrapper
                .eq(QdesNameList::getProjectId, qdesNamelist.getProjectId())
                .eq(QdesNameList::getStructId, qdesNamelist.getStructId())
                .eq(QdesNameList::getGid, qdesNamelist.getGid())
                .eq(QdesNameList::getVersion, qdesNamelist.getVersion());
        QdesNameList qdesNameListResult = baseMapper.selectOne(updateWrapper);
        if(ObjectUtil.isNull(qdesNameListResult)){
            //创建白名单数据的时候，如果前端没有传是否启用，设置个默认关闭的状态
            if(ObjectUtil.isNull(qdesNamelist.getEnable())){
                qdesNamelist.setEnable(false);
            }
            baseMapper.insert(qdesNamelist);
            map.put("id",qdesNamelist.getId());
            return map;
        }else{
            baseMapper.update(qdesNamelist,updateWrapper);
            map.put("id", qdesNameListResult.getId());
            return map;
        }
    }

    @Override
    public QdesNameList getNameList(String projectId, String gid, String version) {
        //创建修改条件对象
        LambdaUpdateWrapper<QdesNameList> updateWrapper = new LambdaUpdateWrapper<>();
        //构造查询条件
        updateWrapper
                .eq(QdesNameList::getProjectId, projectId)
                .eq(QdesNameList::getGid, gid)
                .eq(QdesNameList::getVersion, version);
        QdesNameList qdesNameListResult = baseMapper.selectOne(updateWrapper);
        return qdesNameListResult;
    }

}
